﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Catering_DBOpertion;
using System.Data;
using System.Data.SqlClient;

namespace Catering_Operation
{
    public class LaborCosts
    {
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM LaborCosts");
            strSql.Append(" WHERE ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public bool Exists(int groupid, int classid, DateTime date)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM LaborCosts");
            strSql.Append(" WHERE GroupID=@GroupID AND ClassID=@ClassID AND YEAR(Date)=@YDate AND MONTH(Date)=@MDate ");
            SqlParameter[] parameters = {
					new SqlParameter("@GroupID", SqlDbType.Int,4),
                    new SqlParameter("@ClassID", SqlDbType.Int,4),
                    new SqlParameter("@YDate", SqlDbType.Int,4),
                    new SqlParameter("@MDate", SqlDbType.Int,4)};
            parameters[0].Value = groupid;
            parameters[1].Value = classid;
            parameters[2].Value = date.Year;
            parameters[3].Value = date.Month;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Catering_Model.LaborCostsInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO LaborCosts(");
            strSql.Append("Date,Number,ClassID,GroupID,CompanyID)");
            strSql.Append(" VALUES (");
            strSql.Append("@Date,@Number,@ClassID,@GroupID,@CompanyID)");
            SqlParameter[] parameters = {
					new SqlParameter("@Date", SqlDbType.DateTime,8),
					new SqlParameter("@Number", SqlDbType.Decimal,20),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@GroupID", SqlDbType.Int,4),
					new SqlParameter("@CompanyID", SqlDbType.Int,4)};
            parameters[0].Value = model.Date;
            parameters[1].Value = model.Number;
            parameters[2].Value = model.ClassID;
            parameters[3].Value = model.GroupID;
            parameters[4].Value = model.CompanyID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.LaborCostsInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE LaborCosts SET ");
            strSql.Append("Date=@Date,");
            strSql.Append("Number=@Number,");
            strSql.Append("ClassID=@ClassID,");
            strSql.Append("GroupID=@GroupID");
            strSql.Append("CompanyID=@CompanyID");
            strSql.Append(" WHERE ID=@ID ");
            SqlParameter[] parameters = {
                    new SqlParameter("@Date", SqlDbType.DateTime,8),
					new SqlParameter("@Number", SqlDbType.Decimal,20),
					new SqlParameter("@ClassID", SqlDbType.Int),
					new SqlParameter("@GroupID", SqlDbType.Int,50),
					new SqlParameter("@CompanyID", SqlDbType.Int,50),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.Date;
            parameters[1].Value = model.Number;
            parameters[2].Value = model.ClassID;
            parameters[3].Value = model.GroupID;
            parameters[4].Value = model.CompanyID;
            parameters[5].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateByWhere(Catering_Model.LaborCostsInfo model)
        {
            string sql = "UPDATE LaborCosts SET Number=@Number WHERE GroupID=@GroupID AND ClassID=@ClassID AND YEAR(Date)=@YDate AND MONTH(Date)=@MDate ";
            SqlParameter[] parameters = {
					new SqlParameter("@GroupID", SqlDbType.Int,4),
                    new SqlParameter("@ClassID", SqlDbType.Int,4),
                    new SqlParameter("@YDate", SqlDbType.Int,4),
                    new SqlParameter("@MDate", SqlDbType.Int,4),
                    new SqlParameter("@Number", SqlDbType.Decimal,20)};
            parameters[0].Value = model.GroupID;
            parameters[1].Value = model.ClassID;
            parameters[2].Value = model.Date.Year;
            parameters[3].Value = model.Date.Month;
            parameters[4].Value = model.Number;

            int rows = DbHelperSQL.ExecuteSql(sql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM LaborCosts ");
            strSql.Append(" WHERE ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM LaborCosts ");
            strSql.Append(" WHERE ID IN (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.LaborCostsInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT TOP 1 ID,Date,Number,ClassID,GroupID,CompanyID FROM LaborCosts ");
            strSql.Append(" WHERE ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Catering_Model.LaborCostsInfo model = new Catering_Model.LaborCostsInfo();
            DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                if (!(dt.Rows[0]["ID"] is DBNull) && dt.Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(dt.Rows[0]["ID"].ToString());
                }
                if (!(dt.Rows[0]["Date"] is DBNull) && dt.Rows[0]["Date"].ToString() != "")
                {
                    model.Date = Convert.ToDateTime(dt.Rows[0]["Date"]);
                }
                if (!(dt.Rows[0]["Number"] is DBNull) && dt.Rows[0]["Number"].ToString() != "")
                {
                    model.Number = Convert.ToDecimal(dt.Rows[0]["Number"]);
                }
                if (!(dt.Rows[0]["ClassID"] is DBNull) && dt.Rows[0]["ClassID"].ToString() != "")
                {
                    model.ClassID = Convert.ToInt32(dt.Rows[0]["ClassID"]);
                }
                if (!(dt.Rows[0]["GroupID"] is DBNull) && dt.Rows[0]["GroupID"].ToString() != "")
                {
                    model.GroupID = Convert.ToInt32(dt.Rows[0]["GroupID"]);
                }
                if (!(dt.Rows[0]["CompanyID"] is DBNull) && dt.Rows[0]["CompanyID"].ToString() != "")
                {
                    model.CompanyID = Convert.ToInt32(dt.Rows[0]["CompanyID"]);
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="groupid"></param>
        /// <param name="classid"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public Catering_Model.LaborCostsInfo GetModel(int groupid, int classid, DateTime date)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT TOP 1 ID,Date,Number,ClassID,GroupID,CompanyID FROM LaborCosts ");
            strSql.Append(" WHERE GroupID=@GroupID AND ClassID=@ClassID AND YEAR(Date)=@YDate AND MONTH(Date)=@MDate ");
            SqlParameter[] parameters = {
					new SqlParameter("@GroupID", SqlDbType.Int,4),
                    new SqlParameter("@ClassID", SqlDbType.Int,4),
                    new SqlParameter("@YDate", SqlDbType.Int,4),
                    new SqlParameter("@MDate", SqlDbType.Int,4)};
            parameters[0].Value = groupid;
            parameters[1].Value = classid;
            parameters[2].Value = date.Year;
            parameters[3].Value = date.Month;

            Catering_Model.LaborCostsInfo model = new Catering_Model.LaborCostsInfo();
            DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                if (!(dt.Rows[0]["ID"] is DBNull) && dt.Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(dt.Rows[0]["ID"].ToString());
                }
                if (!(dt.Rows[0]["Date"] is DBNull) && dt.Rows[0]["Date"].ToString() != "")
                {
                    model.Date = Convert.ToDateTime(dt.Rows[0]["Date"]);
                }
                if (!(dt.Rows[0]["Number"] is DBNull) && dt.Rows[0]["Number"].ToString() != "")
                {
                    model.Number = Convert.ToDecimal(dt.Rows[0]["Number"]);
                }
                if (!(dt.Rows[0]["ClassID"] is DBNull) && dt.Rows[0]["ClassID"].ToString() != "")
                {
                    model.ClassID = Convert.ToInt32(dt.Rows[0]["ClassID"]);
                }
                if (!(dt.Rows[0]["GroupID"] is DBNull) && dt.Rows[0]["GroupID"].ToString() != "")
                {
                    model.GroupID = Convert.ToInt32(dt.Rows[0]["GroupID"]);
                }
                if (!(dt.Rows[0]["CompanyID"] is DBNull) && dt.Rows[0]["CompanyID"].ToString() != "")
                {
                    model.CompanyID = Convert.ToInt32(dt.Rows[0]["CompanyID"]);
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            string strSql = "SELECT ID,Date,Number,ClassID,GroupID,CompanyID FROM LaborCosts WHERE 1=1 " + strWhere;
            return DbHelperSQL.Query(strSql);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ");
            if (Top > 0)
            {
                strSql.Append(" TOP " + Top.ToString());
            }
            strSql.Append(" ID,Date,Number,ClassID,GroupID,CompanyID ");
            strSql.Append(" FROM LaborCosts ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE 1=1 " + strWhere);
            }
            strSql.Append(" ORDER BY " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}
